﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EZCRS.HCMS.Entity;
using EZCRS.HCMS.SqlClient;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using EZCRS.HCMS.Bll.baseinfo;

namespace EZCRS.HCMS.Bll.userinfo
{
    public class Blluser:MainBase
    {
        private string ErrMsg = "";

        /// <summary>
        /// 管理员状态
        /// </summary>
        /// <returns></returns>
        public DataTable AdminStatus()
        {
            string[] LogData = Common.DoubleStatus;

            DataTable dt = new DataTable();
            DataColumn dc1 = dt.Columns.Add("sid", Type.GetType("System.String"));
            DataColumn dc2 = dt.Columns.Add("sname", Type.GetType("System.String"));


            for (int i = 0; i < LogData.Length; i++)
            {
                DataRow dr = dt.NewRow();
                dr["sid"] = LogData[i].Split(',')[0];
                dr["sname"] = LogData[i].Split(',')[LanguageNumber+1];

                dt.Rows.Add(dr);
            }
            return dt;
        }
        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrMsgs
        {
            get { return ErrMsg; }
        }

        #region 处理登录
        /// <summary>
        /// 处理登录
        /// </summary>
        /// <param name="UsrName">用户名</param>
        /// <param name="UsrPassWord">密码</param>
        /// <param name="SavDay">保存天数</param>
        /// <returns></returns>
        public bool LoginIn(string UsrName, string UsrPassWord, string SavDay, out string hid, out string gid, out string admid)
        {
            hid = "0";
            gid = "0";
            admid = "0";
            UsrPassWord = CFun.DESEncrypt(UsrPassWord, CFun.BackKey);
            UsrName = CFun.VerifySQL(UsrName);
            string Ip = CFun.GetClientIP();

            
            string AdmId = "";
            if (new sql_AdminUser().CheckUser(UsrName,UsrPassWord,Ip))
            {
                DataTable dt = new sql_AdminUser().GetLoginUser(UsrName);
                AdmId = dt.Rows[0]["admid"].ToString();

                DataSet ds = new BllAdminInfo().WebAdminInfo_Item(AdmId);
                string helptype = "1";
                if (ds.Tables[0].Rows.Count == 1)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    helptype = dr["helptype"].ToString();
                }

                
                HttpCookie cookie1 = new HttpCookie("cms");
                cookie1.Values["n"] = UsrName;
                cookie1.Values["d"] = CFun.DESEncrypt(dt.Rows[0]["rolid"].ToString(), CFun.CookieKey);
                cookie1.Values["l"] = CFun.DESEncrypt(dt.Rows[0]["rolmodules"].ToString(), CFun.CookieKey);
                cookie1.Values["i"] = AdmId;
                cookie1.Values["t"] = dt.Rows[0]["orgtype"].ToString();
                cookie1.Values["oh"] = helptype;

                cookie1.Values["r"] = dt.Rows[0]["roltype"].ToString();


                if (dt.Rows[0]["aorgtype"].ToString() == "0")
                {
                    cookie1.Values["h"] = "0";
                    cookie1.Values["g"] = dt.Rows[0]["aorgid"].ToString();
                }
                else if (dt.Rows[0]["aorgtype"].ToString() == "3")
                {
                    cookie1.Values["h"] = "0";
                    cookie1.Values["g"] = "0";
                    cookie1.Values["p"] = "0";
                }
                else
                {
                    cookie1.Values["h"] = dt.Rows[0]["aorgid"].ToString();
                    cookie1.Values["p"] = "0";
                    cookie1.Values["g"] = "0";
                }

                hid = cookie1.Values["h"];
                gid = cookie1.Values["g"];
                admid = cookie1.Values["i"];
                if (SavDay != "0")
                    cookie1.Expires = DateTime.Now.AddDays(double.Parse(SavDay));
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie1);

                //处理首选语言
                if (AdmId != "")
                {
                    string rolestr = "";
                    if (new sql_AdminUser().CheckUserThree(AdmId,out rolestr))
                    {
                        initlanguagetype(Common.languagecode(rolestr));
                    }
                }
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 维护首选语言
        /// </summary>
        /// <param name="types">语言类型</param>
        public static void initlanguagetype(string types)
        {
            HttpCookie co1 = new HttpCookie("clang");
            co1.Value = types;
            System.Web.HttpContext.Current.Response.Cookies.Add(co1);
        }
        #endregion

        #region 后台用户相关；添加、删除、修改、权限分配

        /// <summary>
        /// 生成随即密码 5 位数字
        /// </summary>
        /// <returns></returns>
        public static string CreatePassword()
        {
            Random r = new Random();
            int rl = r.Next(10000, 99999);
            return rl.ToString();
        }


        /// <summary>
        /// 返回用户名是否重复,如果存在，则返回true
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public bool User_Have(string UserName)
        {
            UserName = CFun.VerifySQL(UserName);
            return new sql_AdminUser().User_Have(UserName);
        }
        /// <summary>
        /// 添加或修改管理用户
        /// </summary>
        /// <param name="AdmId">管理员编号  0=为新增管理员 其他为修改</param>
        /// <param name="OrgId">集团编号</param>
        /// <param name="Orgtype">集团类型</param>
        /// <param name="RolId">角色编号</param>
        /// <param name="LogName">登录名</param>
        /// <param name="LogPassword">登录密码</param>
        /// <param name="Username">用户真实姓名</param>
        /// <param name="UserEmail">电子邮箱</param>
        /// <param name="UsrContact">联系方式</param>
        /// <param name="AdmStatus">状态 0=正常 1=暂停</param>
        /// <param name="InUser">录入人</param>
        /// <returns></returns>
        public bool User_Add(int AdmId, int OrgId, int Orgtype, string RolId, string LogName, string LogPassword, string Username, string UserEmail, string UsrContact, string AdmStatus, string InUser)
        {
            LogPassword=CFun.DESEncrypt(LogPassword, CFun.BackKey);

            return new sql_AdminUser().User_Add(AdmId,OrgId,Orgtype,RolId,LogName,LogPassword,Username,UserEmail,UsrContact,AdmStatus,InUser);
        }

        /// <summary>
        /// 密码初始化
        /// </summary>
        /// <param name="UId">管理员id</param>
        /// <returns></returns>
        public string User_InitPass(string UId)
        {
            string Pass = CreatePassword();
            UId = CFun.VerifySQL(UId);
            new sql_AdminUser().User_InitPass(UId, CFun.DESEncrypt(Pass, CFun.BackKey));
            return Pass;
        }
        /// <summary>
        /// 管理员列表
        /// </summary>
        /// <param name="OrgId">组织编号</param>
        /// <param name="OrgType">组织类型 0=集团  1=酒店 3=hubs1</param>
        /// <returns></returns>
        public DataTable User_List(string OrgId, string OrgType)
        {
            OrgId = CFun.VerifySQL(OrgId);
            OrgType = CFun.VerifySQL(OrgType);
            return new sql_AdminUser().User_List(OrgId, OrgType);
        }

        /// <summary>
        /// 删除管理用户
        /// </summary>
        /// <param name="AId">用户id</param>
        /// <returns></returns>
        public bool User_Del(string AId)
        {
            AId = CFun.VerifySQL(AId);

            return new sql_AdminUser().User_Del(AId);
        }

        /// <summary>
        /// 返回一条管理员信息
        /// </summary>
        /// <param name="UId">管理员id</param>
        /// <returns></returns>
        public DataRow User_Item(int UId, int GId)
        {
            return new sql_AdminUser().User_Item(UId, GId);
        }
        public DataRow User_Item(string UName)
        {
            UName = CFun.VerifySQL(UName);

            return new sql_AdminUser().User_Item(UName);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="UserId">用户id</param>
        /// <param name="OldPassWord">旧密码</param>
        /// <param name="NewPassWord">新密码</param>
        /// <returns></returns>
        public bool User_Pass(string UserId, string OldPassWord, string NewPassWord)
        {
            NewPassWord = CFun.DESEncrypt(NewPassWord, CFun.BackKey);
            OldPassWord = CFun.DESEncrypt(OldPassWord, CFun.BackKey);
            UserId = CFun.VerifySQL(UserId);

            return new sql_AdminUser().User_Pass(UserId, OldPassWord, NewPassWord);
        }
        #endregion

        #region
        #endregion

        #region 角色相关 添加、删除、修改
        /// <summary>
        /// 添加或修改角色
        /// </summary>
        /// <param name="RolId">角色id 0为添加</param>
        /// <param name="RolName">角色名称</param>
        /// <param name="ModuleId">模块编号</param>
        /// <param name="HotelList">酒店列表</param>
        /// <param name="RolType">模块类别</param>
        /// <param name="RolComment">描述</param>
        /// <returns></returns>
        public bool Role_Add(int RolId, string RolName, string ModuleId, string HotelList, string GroupList, string RolType, string RolComment, string OrgId, string OrgType)
        {
            return new sql_AdminUser().Role_Add(RolId, RolName, ModuleId, HotelList, GroupList, RolType, RolComment, OrgId, OrgType);
        }

        /// <summary>
        /// 返回所有角色
        /// </summary>
        /// <param name="OrgId">组织编号 0 为超级管理员</param>
        /// <param name="OrgType">组织类型 0=集团 1=酒店 3=hubs1</param>
        /// <returns></returns>
        public DataTable Role_List(string OrgId, string OrgType)
        {
            return new sql_AdminUser().Role_List(OrgId, OrgType);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="RolId">角色id</param>
        /// <returns></returns>
        public bool Role_Del(int RolId)
        {
            return new sql_AdminUser().Role_Del(RolId);
        }

        /// <summary>
        /// 返回一条角色信息
        /// </summary>
        /// <param name="RolId">角色id</param>
        /// <returns></returns>
        public DataRow Role_Item(string RolId, int GId)
        {
            RolId = CFun.VerifySQL(RolId);

            return new sql_AdminUser().Role_Item(RolId, GId);
        }
        /// <summary>
        /// 返回角色能够访问的酒店
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public string Role_Hotel(string RoleId)
        {
            RoleId = CFun.VerifySQL(RoleId);
            return new sql_AdminUser().Role_Hotel(RoleId);
        }

        /// <summary>
        /// 返回角色能访问的集团
        /// </summary>
        /// <param name="RolId"></param>
        /// <returns></returns>
        public string Role_Group(string RolId)
        {
            RolId = CFun.VerifySQL(RolId);
            return new sql_AdminUser().Role_Group(RolId);
        }
        #endregion

        #region
        #endregion


        /////////////////////////////////////////


       
        #region 信用卡信息列表
        public DataTable CreditCard_List(string Uid)
        {
            Uid = CFun.VerifySQL(Uid);
            return new sql_AdmCreditCard().CreditCard_List(Uid);
        }
        #endregion
        #region 信用卡信息添加、修改
        /// <summary>
        /// 修改或添加信用卡信息
        /// </summary>
        /// <returns></returns>
        public bool CreditCard_Add(string Uid, string Creditcardtype, string Creditcardno, string Expirationdate, string Cardname, string Indate, string Cid)
        {
            Cid = CFun.VerifySQL(Cid);
            return new sql_AdmCreditCard().CreditCard_Add(Uid, Creditcardtype, Creditcardno, Expirationdate, Cardname, Indate, Cid);
        }
        #endregion
        #region 信用卡Info数据的显示,显示一条数据
        public DataRow CreditCard_Item(string Cid)
        {
            Cid = CFun.VerifySQL(Cid);
            return new sql_AdmCreditCard().CreditCard_Item(Cid);
        }
        #endregion
        #region 信用卡的删除
        public bool CreditCard_Del(string Cid)
        {
            Cid = CFun.VerifySQL(Cid);
            return new sql_AdmCreditCard().CreditCard_Del(Cid);
        }
        #endregion


        ///////////////////////////////
    
        /// <summary>
        /// 保存级别等级
        /// </summary>
        /// <param name="levelid"></param>
        /// <param name="levelsort"></param>
        /// <returns></returns>
        public bool savelevelsort(int levelid, int levelsort)
        {
            return new sql_AdmLevelSetUp().savelevelsort(levelid, levelsort);
        }
        #region 常客等级设置信息列表
        /// <summary>
        /// 返回所有常客等级
        /// </summary>
        /// <param name="OrgId">组织编号 0 为超级管理员</param>
        /// <param name="OrgType">组织类型 0=集团 1=酒店 3=hubs1</param>
        /// <returns></returns>
        public DataTable LevelSetUp_List(string OrgId, string OrgType)
        {
            OrgId = CFun.VerifySQL(OrgId);
            OrgType = CFun.VerifySQL(OrgType);

            return new sql_AdmLevelSetUp().LevelSetUp_List(OrgId, OrgType);
        }
        #endregion
        #region 等级设置信息添加、修改
        /// <summary>
        /// 修改或添加等级设置信息
        /// </summary>
        /// <returns></returns>
        public bool LevelSetUp_Add(string Orgid, string Orgtype, string Lvlname, string Lvlimg, string Startscore, string Endscore, string Days, string Hotels, string Lvlcomment, string Inuser, string Indate, string Lvlcount, string Lvlid)
        {
            Lvlid = CFun.VerifySQL(Lvlid);
            return new sql_AdmLevelSetUp().LevelSetUp_Add(Orgid, Orgtype, Lvlname, Lvlimg, Startscore, Endscore, Days, Hotels, Lvlcomment, Inuser, Indate, Lvlcount, Lvlid);
        }
        #endregion
        #region 等级设置Info数据的显示,显示一条数据
        public DataRow LevelSetUp_Item(string Lvlid)
        {
            Lvlid = CFun.VerifySQL(Lvlid);
            return new sql_AdmLevelSetUp().LevelSetUp_Item(Lvlid);
        }
        #endregion
        #region 等级设置的删除
        public bool LevelSetUp_Del(string Lvlid)
        {
            Lvlid = CFun.VerifySQL(Lvlid);
            return new sql_AdmLevelSetUp().LevelSetUp_Del(Lvlid);
        }
        #endregion
    }
}
